package com.alibaba.dongbao.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @Author: Mr.ZJW
 * @Date: 2022-05-06 9:30
 * @Description: 获取请求参数工具类，不管是get或post
 */
public class HttpParamUtil {

    /**
     * @param [request]
     * @Author: Mr.ZJW
     * @Description: 获取请求中的所以参数，包括get或post
     * @Date: 2022/5/6 10:25
     **/
    public static SortedMap<String, Object> getAllParams(HttpServletRequest request) throws IOException {
        //总的参数map
        SortedMap<String, Object> allMap = new TreeMap<>();
        //获取URL上的参数
        if (StringUtils.isNotEmpty(request.getQueryString())) {
            Map<String, Object> urlParams = getUrlParams(request);
            //遍历URL上的参数
            for (Map.Entry entry : urlParams.entrySet()) {
                allMap.put((String) entry.getKey(), entry.getValue());
            }
        }
        //获取Body上的参数
        Map<String, String> bodyParams = getBodyParams(request);
        if (ObjectUtils.isNotEmpty(bodyParams)) {
            //遍历Body上的参数
            for (Map.Entry entry : bodyParams.entrySet()) {
                allMap.put((String) entry.getKey(), entry.getValue());
            }
        }
        return allMap;
    }

    /**
     * @param [request]
     * @Author: Mr.ZJW
     * @Description: 获取Body上的参数
     * @Date: 2022/5/6 9:52
     **/
    private static Map<String, String> getBodyParams(HttpServletRequest request) throws IOException {
        //读取Body中的参数
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String s = "";
        while (null != (s = bufferedReader.readLine())) {
            sb.append(s);
        }
        //转Map
        return JSONObject.parseObject(sb.toString(), Map.class);
    }

    /**
     * @param [request]
     * @Author: Mr.ZJW
     * @Description: 获取URL上的参数
     * @Date: 2022/5/6 9:52
     **/
    private static Map<String, Object> getUrlParams(HttpServletRequest request) {
        String queryParam = "";
        try {
            //查询URL上的请求参数
            queryParam = URLDecoder.decode(request.getQueryString(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HashMap<String, Object> result = new HashMap<>();
        //分隔：如//hello?a=1&b=2  分隔&
        String[] split = queryParam.split("&");
        //遍历
        for (String s : split) {
            int i = s.indexOf("=");
            result.put(s.substring(0, i), s.substring(i + 1));
        }
        return result;
    }

}
